package com.liang.maogouyuyin.local

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.liang.maogouyuyin.bean.AnomalyBean
import com.liang.maogouyuyin.bean.CasebookBean
import com.liang.maogouyuyin.bean.ExpenditureBean
import com.liang.maogouyuyin.bean.PetBean
import com.liang.maogouyuyin.bean.WeightBean
import com.liang.maogouyuyin.local.dao.AnomalyDao
import com.liang.maogouyuyin.local.dao.CasebookDao
import com.liang.maogouyuyin.local.dao.ExpenditureDao
import com.liang.maogouyuyin.local.dao.PetDao
import com.liang.maogouyuyin.local.dao.WeightDao

@Database(entities = [PetBean::class, ExpenditureBean::class, WeightBean::class, AnomalyBean::class, CasebookBean::class], version = 1, exportSchema = false)
abstract class AppDatabase: RoomDatabase() {

    abstract fun petDao(): PetDao

    abstract fun expenditureDao(): ExpenditureDao

    abstract fun weightDao(): WeightDao

    abstract fun anomalyDao(): AnomalyDao

    abstract fun casebookDao(): CasebookDao

    companion object {
        @Volatile
        private var instance: AppDatabase? = null

        fun getInstance(context: Context): AppDatabase {
            return instance?: synchronized(this) {
                instance?: buildDatabase(context).also {
                    instance = it
                }
            }
        }

        private fun buildDatabase(context: Context): AppDatabase {
            return Room.databaseBuilder(
                context,
                AppDatabase::class.java,
                "petDatabase"
            ).build()
        }
    }
}